#include <stdio.h>
#include <assert.h>  
#include <stdlib.h>
#include <iostream>  
#include "leveldb/db.h"  
#include "leveldb/options.h"
#include "leveldb/cache.h"
  
using namespace std;  
  
typedef struct _stValue
{
	int m_i_a;
	int m_i_b;
	int m_i_c;
	int m_i_d;
}TValue;

int main(int argc,char * argv[])  
{  
	leveldb::DB* db;
	leveldb::Options options;

	options.block_cache = leveldb::NewLRUCache(30*1024*1024);
	options.create_if_missing = true;

	leveldb::Status status = leveldb::DB::Open(options, "testdb", &db);
	assert(status.ok());

	leveldb::Status s;
#if 1
	leveldb::WriteOptions write_opt;
	write_opt.sync = false;
	time_t start_w = time(0);
	for( int i = 0;i < 1000000; i++)
	{
		char chKey[10] = {0};
		snprintf(chKey, 10, "db_%d",i);
		TValue vvv;
		vvv.m_i_a = i;
		vvv.m_i_b = i+1;
		vvv.m_i_c = i+i;
		vvv.m_i_d = time(0);

		leveldb::Slice slice_value;
		db->Put(write_opt,
				chKey,
				leveldb::Slice((char*)&vvv,
				sizeof(TValue)));
	}
	time_t end_w = time(0);
	printf("Used %ld second!", end_w - start_w);
#endif
#if 0
	TValue *ggg;
	srand(time(0));
	time_t start = time(0);
	leveldb::ReadOptions read_opt;
	read_opt.fill_cache = true;
	for(int j = 0; j < 1000000; j++)
	{
		char chKey[10] = {0};
		int k = rand()%1000000;
		snprintf(chKey, 10, "db_%d",k);

		std::string value;
		db->Get(read_opt, chKey, &value);

		ggg = (TValue*)value.c_str();
		time_t end = time(0);
		if (end - start >= 1)
		{
			printf("scan %d data\n", j);
			printf("DATA\n"
				   "m_i_a = %d\n"
				   "m_i_b = %d\n"
				   "m_i_c = %d\n"
				   "m_i_d = %d\n",
					ggg->m_i_a,
					ggg->m_i_b,
					ggg->m_i_c,
					ggg->m_i_d);
			j = 0;
			start = end;
		}
	}
#endif

	delete db;
	delete options.block_cache;
	return 0;  
}  
